//
//  LYThreeLineDashedView.swift
//  LYOnlineBanking
//
//  Created by 李永彬 on 2025/7/31.
//

import UIKit
import SnapKit

class LYThreeLineDashedView: UIView {
    
    // MARK: - UI Elements
    private let stackView: UIStackView = {
        let stack = UIStackView()
        stack.axis = .vertical
        stack.distribution = .fillEqually
        stack.spacing = 0
        return stack
    }()
    
    private var rows: [LYLabelRow] = []
    
    // MARK: - Initialization
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - Configuration
    func configure(
        topTitle: String, topContent: String,
        middleTitle: String, middleContent: String,
        bottomTitle: String, bottomContent: String
    ) {
        rows[0].configure(title: topTitle, content: topContent)
        rows[1].configure(title: middleTitle, content: middleContent)
        rows[2].configure(title: bottomTitle, content: bottomContent)
    }
    
    // MARK: - Setup
    private func setupUI() {
        addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        // Create 3 rows
        for i in 0..<3 {
            let row = LYLabelRow()
            if i < 2 {
                row.addSeparator()
            }
            rows.append(row)
            stackView.addArrangedSubview(row)
        }
    }
}

private class LYLabelRow: UIView {
    private let titleLabel: UILabel = {
        let label = UILabel()
        label.font = .systemFont(ofSize: 16, weight: .medium)
        label.textColor = .darkGray
        label.setContentHuggingPriority(.required, for: .horizontal)
        return label
    }()
    
    private let contentLabel: UILabel = {
        let label = UILabel()
        label.font = .systemFont(ofSize: 16)
        label.textColor = .darkGray
        label.numberOfLines = 0
        return label
    }()
    
    private let separator = UIView()
    
    init() {
        super.init(frame: .zero)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func configure(title: String, content: String) {
        titleLabel.text = title
        contentLabel.text = content
    }
    
    func addSeparator() {
        let dashedLine = createDashedLine()
        addSubview(dashedLine)
        dashedLine.snp.makeConstraints { make in
            make.leading.trailing.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
    }
    
    private func setupUI() {
        addSubview(titleLabel)
        addSubview(contentLabel)
        
        titleLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.centerY.equalToSuperview()
        }
        
        contentLabel.snp.makeConstraints { make in
            make.leading.equalTo(titleLabel.snp.trailing).offset(8)
            make.trailing.equalToSuperview().offset(-16)
            make.centerY.equalToSuperview()
            make.top.greaterThanOrEqualToSuperview().offset(8)
            make.bottom.lessThanOrEqualToSuperview().offset(-8)
        }
    }
    
    private func createDashedLine() -> UIView {
        let view = UIView()
        view.backgroundColor = .clear
        
        let shapeLayer = CAShapeLayer()
        shapeLayer.strokeColor = UIColor.lightGray.cgColor
        shapeLayer.lineWidth = 1
        shapeLayer.lineDashPattern = [4, 2]
        
        let path = CGMutablePath()
        path.addLines(between: [CGPoint(x: 16, y: 0),
                             CGPoint(x: UIScreen.main.bounds.width - 32, y: 0)])
        shapeLayer.path = path
        
        view.layer.addSublayer(shapeLayer)
        return view
    }
}
